package com.interagriculture.controller;

import com.interagriculture.annotation.AdminVerify;
import com.interagriculture.common.Result;
import com.interagriculture.entity.Journalism;
import com.interagriculture.entity.JournalismMain;
import com.interagriculture.service.JournalismMainService;
import com.interagriculture.service.JournalismService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.HashMap;

/**
 * @author 11 2024/11/14
 */
@RestController
@RequestMapping("/adminJournalism")
public class AdminJournalismController {

    @Resource
    private JournalismService journalismService;
    @Resource
    private JournalismMainService journalismMainService;

    @PostMapping("/saveOrUpdate")
    @AdminVerify
    public Result saveOrUpdate(@RequestBody Journalism journalism){
        Integer journalismId = journalismService.saveOrUpdate(journalism);
        HashMap<String, Integer> map = new HashMap<>();
        map.put("journalismId",journalismId);
        return Result.ok(map);
    }

    @PostMapping("/delete")
    @AdminVerify
    public Result delete(@RequestParam Integer id){
        boolean deleteRes = journalismService.deleteById(id);
        return Result.ok();
    }

    @PostMapping("/saveOrUpdateMain")
    @AdminVerify
    public Result saveOrUpdateMain(@NotNull Integer journalismId,
                                   Integer id,
                                   String mainBody,
                                   @RequestPart(required = false) MultipartFile picture){
        JournalismMain journalismMain = new JournalismMain();
        journalismMain.setJournalismId(journalismId);
        journalismMain.setMainBody(mainBody);
        journalismMain.setId(id);
        journalismMainService.saveOrUpdateMain(journalismMain,picture);
        return Result.ok();

    }





    public static void main(String[] args) {
        //测试时间格式化
        LocalDateTime now = LocalDateTime.now();
        System.out.println(now);
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
        System.out.println(now.format(dateTimeFormatter));

        //2024-11-05 16:02:05
    }

}
